package com.example.demo;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static tools.print.*;

//零例 ， 直接用类名调用静态函数
public class Connector {

    static final String dbUrl = "jdbc:mysql://47.114.48.8:3306/Forum?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC&characterEncoding=UTF-8";
    static final String user = "root";
    static final String paswd = "12345678";

    private
        static Connection conn;
        static Statement stmt;
        Connector() {}

    public static void tt() {
        pln("hello");
    }

    //初始化
    public static void init() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        pln("connect to db");
        conn = DriverManager.getConnection(dbUrl,user,paswd);
        pln("init of statement");
        stmt = conn.createStatement();
    }

    //查询
    //获取查询集，解析返回的结构（返回值及类型）
    public static List query(String sql) throws SQLException
    {
        //查询结果集
        ResultSet res = stmt.executeQuery(sql);
        //获取表结构
        ResultSetMetaData  resd=res.getMetaData();
        //行数
        int cols = resd.getColumnCount();
        //str0[] type ; str1[] name
        String[] str0 = new String[cols];
        String[] str1 = new String[cols];
        for(int i=1; i<=cols; i++) {
            str0[i - 1] = (String) resd.getColumnTypeName(i);
            str1[i - 1] = (String) resd.getColumnName(i);
        }
        return analysis(res , str0 , str1);
    }
    private  static List analysis(ResultSet res , String[] str0 , String[] str1) throws SQLException {
        for(int i=0; i<str0.length; i++)
        {
            pln("\n");
            pln(str0[i]);
            pln("  +  "+str1[i]);
        }
        List list =  new ArrayList<>();
        while(res.next())
        {
            Map mapt = new HashMap();
            // 通过字段检索
            for(int i=0; i<str0.length; i++)
            {
                mapt.putAll(varb(res, str0[i], str1[i]));
            }
            list.add(mapt);
            pln(list);
        }
        return list;
    }
    private static Map varb(ResultSet res , String type , String name) throws SQLException {
        //根据数据类型，调用不同语句获取查询结果
        Map map = new HashMap();
        if(type.equals("INT"))
        {
            map.put(name,res.getInt(name));
            return map;
        }
        if(type.equals("DATE"))
        {
            map.put(name,res.getDate(name));
            return map;
        }
        if(type.equals("VARCHAR"))
        {
            map.put(name,res.getString(name));
            return map;
        }
        pln("\n\n"+"Unknow type !"+"\n\n");
        return map;
    }

    //插入
    public static int insert(String sql) throws SQLException
    {
        int impact = stmt.executeUpdate(sql);
        if(impact != 0)
            pln("Insert done");
        else
            pln("Insert error");
        return impact;
    }

    //修改
    public static int updata(String sql) throws SQLException {
        int impact = stmt.executeUpdate(sql);
        if(impact != 0)
            pln("updata done");
        else
            pln("updata error");
        return impact;
    }

    //删除
    public static int delete(String sql) throws SQLException {
        int impact = stmt.executeUpdate(sql);
        if(impact != 0)
            pln("delete done");
        else
            pln("delete error");
        return impact;
    }

    public static boolean

}
